package com.jlect.swebing.ui.client;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * Act as multi cast for the change events
 *
 * @author Sergey Kozmin
 * @since 15.11.2007 11:35:36
 */
public class ChangeListenerCollection extends ArrayList {
    private ContentChangeEvent contentEvent;
    private StateChangeEvent stateEvent;
    private SelectionChangeEvent selectionEvent;

    public void changedPerformed(ChangeEvent event) {
        for (Iterator iterator = iterator(); iterator.hasNext();) {
            ChangeListener listener = (ChangeListener) iterator.next();
            listener.changePerformed(event);
        }
    }

    public void contentChanged(GComponent component) {
        if(contentEvent == null) {
            contentEvent = new ContentChangeEvent(component);
        } else {
            contentEvent.setSource(component);
        }
        changedPerformed(contentEvent);
    }

    public void stateChanged(GComponent component) {
        if(stateEvent == null) {
            stateEvent = new StateChangeEvent(component);
        } else {
            stateEvent.setSource(component);
        }
        changedPerformed(stateEvent);
    }

    public void selectionChanged(GComponent component) {
        if(selectionEvent == null) {
            selectionEvent = new SelectionChangeEvent(component);
        } else {
            selectionEvent.setSource(component);
        }
        changedPerformed(selectionEvent);
    }
}